package com.smartbone.compenent;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.servlet.ServletException;

import com.smartbone.entity.Users;
import com.smartbone.pagination.DataPage;
import com.smartbone.pagination.PageListBaseBean;
import com.smartbone.pagination.PagedListDataModel;
import com.smartbone.services.interfaces.UserService;

@ManagedBean(name="userListForm")  
@SessionScoped
public class UserListForm extends PageListBaseBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2826645865543848999L;

	@ManagedProperty("#{userService}")
	private UserService userService;

    private int scrollerPage = 1;

    public int getScrollerPage()
    {
        return scrollerPage;
    }

    public void setScrollerPage(int scrollerPage)
    {
        this.scrollerPage = scrollerPage;
        System.out.println("current page = " + scrollerPage);
    }
    
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public List<Users> getAllUserList() throws IOException, ServletException {
				
		return userService.listAllUser();
	}

	@Override
    public PagedListDataModel getDefaultDataModel()
    {
        if ( defaultDataModel == null ) {
            defaultDataModel = new PagedListDataModel(pageSize)
            {
                public DataPage fetchPage(int startRow, int pageSize)
                {
                	List<Users> userList = userService.listAllUserWithPageSize(startRow, pageSize);

                	String count = userService.findAllUserCount();
                    return new DataPage(Integer.parseInt(count), startRow, userList);                    
                }
            };
        }
        
        return defaultDataModel;
    }


}